﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AresCustomYacht
{
    public partial class P_Trade : System.Web.UI.Page
    {
        readonly BL _bl = new BL();
        private int _mode = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            var divisionId = int.Parse(Utility.GetQueryStringItem(this, "ParentId"));
            Utility.DisplayPageMessage(this,lblMessage);

            _mode = Utility.GetPageMode(this);
            if (_mode == 1) //Data mode
            {
                btnAdd.Visible = true;
                btnNext.Visible = true;


            }
            else
            {
                btnAdd.Visible = false;
                btnNext.Visible = true;
            }

            if (!Page.IsPostBack)
            {
                var departments = _bl.GetDivisionTrades(divisionId);
                departments.ForEach(p=> p.Name = p.Code + "  " + p.Name);

                lstTrades.DataSource = departments;
                lstTrades.DataTextField = "Name";
                lstTrades.DataValueField = "Id";
                lstTrades.DataBind();
                lstTrades.SelectedIndex = 0;
            }
            

        }

        protected void lstClass_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            var breadCrump = Utility.GetQueryStringItem(this, "Message");
            int boatId = 0;
            if (_mode != 1) //Data Mode
                boatId = int.Parse(Utility.GetQueryStringItem(this, "BoatId"));

            var linkAddress =
                "Mode="+ _mode +"&BoatId=" + boatId + "&ParentId=" + lstTrades.SelectedValue + "&Message=" + breadCrump + ">" + lstTrades.SelectedItem.Text;
            if (dffBookmark.Checked)
            {
                linkAddress += "&Bookmark=P-Trade.aspx&BookmarkId=" +
                               int.Parse(Utility.GetQueryStringItem(this, "ParentId"));
            }
            else if(!string.IsNullOrEmpty(Utility.GetQueryStringItem(this,"Bookmark")))
            {
                linkAddress += "&Bookmark=" + Utility.GetQueryStringItem(this, "Bookmark") + "&BookmarkId=" +
                               Utility.GetQueryStringItem(this, "BookmarkId");
            }
            Response.Redirect("P-Location.aspx?k=" + Utility.Encrypt(linkAddress));
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("P-BatchAdd.aspx?k=" + Utility.Encrypt("Mode=0&What=Location&ParentId=" + lstTrades.SelectedValue + "&Message=Add related locations to [" + lstTrades.SelectedItem.Text + "]"));

        }
    }
}